#include "read_boy_confg.h"
#include "read_boy_book.h"
#include "ff.h"

//typedef struct book_setting{
//     
//}book_setting_t;
//设置界面
void read_boy_book_init_set_Table_of_Contents(){//更新电子书目录
    
    FRESULT res;  // FatFs返回结果
    DIR dir;      // 目录对象
    FILINFO fno;  // 文件信息结构体
    
    FIL file;  // 文件对象
    UINT bw;
    
    //char temp_buff[20][128];
    //char temp_buff [128];
    char i = 0;
    
    res = f_open(&file,  BOOK_PATH_NEWFILE_TXT, FA_READ);
    if (res == FR_OK) { 
        f_close(&file); 
        //delete_file(BOOK_PATH_NEWFILE_TXT);
        // 删除文件
        res = f_unlink(BOOK_PATH_NEWFILE_TXT);
        if (res != FR_OK) {
            //return res; // 如果删除失败，返回错误码
        }
        
    }
    
    // 尝试打开目录
    res = f_opendir(&dir,  BOOK_PATH_FOLDER);
    if (res != FR_OK) {
        //printf("打开目录失败，错误码：%d\n", res);
        return;
    }
    
     // 以写入模式打开文件，如果不存在则创建
    if (f_open(&file, BOOK_PATH_NEWFILE_TXT, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK) {
        //error("File open failed");
        return;
    }
    
    // 循环读取目录项
    while (1) {
        res = f_readdir(&dir, &fno);  // 读取一个目录项
        if (res != FR_OK || fno.fname[0] == 0) break;  // 出错或到达目录尾部时退出循环
        if (fno.fattrib & AM_DIR) {
            // 这是一个目录
            //draw_string(0,0,16,fno.fname);
        }else{
            // 这是一个文件，尝试打开
            if(strncmp(fno.fname,BOOK_PATH_NEWFILE,strlen(BOOK_PATH_NEWFILE)) == 0){
            
            }else{
                res = f_lseek(&file, i * 128);
                if (res == FR_OK) {
                    //const char *data = "追加的数据\n";
                    res = f_write(&file, fno.fname, 128, &bw); // 写入数据
                    f_sync(&file); // 刷新数据
                }
                
                //strcpy(&temp_buff[i][0],fno.fname);
                i++;
            }
        }

    }
    f_closedir(&dir);  // 关闭目录
    if (f_close(&file) != FR_OK) {
        //error("File close failed");
        return;
    }
    
   
    read_boy_book_total_num_init();
   
}

char read_boy_book_set(){ //选择界面
    static char flag = 0;
    char key_num;
    while(1){
        key_num = readboy_key_scan();
        switch(flag){
            case 0:
                READ_BOY_BOOK_DISP_CLEAR; 
                flag = 1;
            //break;
            case 1:
                draw_string_for_sd_start();
        
                
                //        //read_boy_book_show_one_page_on_sd();
                draw_string_for_sd(my_read_boy_book.iX_initial_value + 40,my_read_boy_book.iY_initial_value,24,"更新电子书目录");
                draw_string_for_sd(my_read_boy_book.iX_initial_value + 40,my_read_boy_book.iY_initial_value + 30,24,"设置时间");
                //my_read_boy_book.old_index = 0;
                //my_read_boy_book.new_index = 0;
                read_boy_draw_char_en(my_read_boy_book.iX_initial_value + 10,my_read_boy_book.iY_initial_value + my_read_boy_book.old_index*30,read_boy_FONT_16x24, '>',0xff);
                read_boy_draw_char_en(my_read_boy_book.iX_initial_value + 10,my_read_boy_book.iY_initial_value + my_read_boy_book.new_index*30,read_boy_FONT_16x24, '>',0);
                READ_BOY_BOOK_DISP;
                
                draw_string_for_sd_end();
                flag = 2;
            break;
            case 2:
                if(key_num == 1){
                    my_read_boy_book.old_index = my_read_boy_book.new_index;
                    my_read_boy_book.new_index++;// = (my_read_boy_book.new_index + 1) % 2;
                    my_read_boy_book.new_index = my_read_boy_book.new_index % 2;
                    flag = 1;
                }
                if(key_num == 2){
                    if(my_read_boy_book.new_index == 0){
                        READ_BOY_BOOK_DISP_CLEAR; 
                        flag =3;
                    }else{
                    
                    }
                }
                
            break;
            case 3:
                draw_string_for_sd_start();
                draw_string_for_sd(my_read_boy_book.iX_initial_value + 40,my_read_boy_book.iY_initial_value,24,"更新电子书目录,请稍等...");
                READ_BOY_BOOK_DISP;
                
                draw_string_for_sd_end();
                
                read_boy_book_init_set_Table_of_Contents();//更新电子书目录
            
                draw_string_for_sd_start();
                draw_string_for_sd(my_read_boy_book.iX_initial_value + 40,my_read_boy_book.iY_initial_value+30,24,"更新电子书目录,OK");
                READ_BOY_BOOK_DISP;
                
                draw_string_for_sd_end();
            
                flag = 4;
            break;
            case 4:
                if(key_num == 3){
                    flag = 0;
                    return key_num;
                }
            break;
        
        }
    }
    return 0;
}























